package com.travel.controller.admin;

import cn.hutool.crypto.digest.BCrypt;
import com.travel.entity.po.User;
import com.travel.entity.vo.UserVo;
import com.travel.result.PageResult;
import com.travel.result.Result;
import com.travel.service.UserService;
import com.travel.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:Jasenon_ce
 * @Date: 2025/03/13/下午3:11
 */
@RestController
@RequestMapping("/admin/user")
@Api(tags = "管理员用户管理")
public class AdminUserController {

    @Autowired
    private UserServiceImpl  userServiceImpl;

    @GetMapping("/getUser")
    public Result<?> getUser(@PathParam("pageNum") Integer pageNum, @PathParam("pageSize") Integer pageSize) {
        // 参数校验
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        // 调用服务获取分页数据
        PageResult<UserVo> pageResult = userServiceImpl.getUsers(pageNum, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping("/override")
    public Result<?> updataUser(@RequestBody User user){

        String encryptPwd = BCrypt.hashpw(user.getPassword());
        user.setPassword(encryptPwd);

        userServiceImpl.updateUser(user);
        return Result.success();
    }

}
